import java.io.*;
import java.lang.reflect.Constructor;

/**
 * 序列化反序列化会造成单例失效
 *
 * @author rcl
 * @date 2019/7/4 15:26
 */
public class Serializable {

    public static void main(String[] args) throws Exception {
//        LazyDoubleCheck s = LazyDoubleCheck.getInstance();
        Hungry s = Hungry.getInstance();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("singleton.obj"));
        oos.writeObject(s);
        oos.flush();
        oos.close();
        Constructor<Hungry> constructor = Hungry.class.getDeclaredConstructor();
        constructor.setAccessible(true);
        //constructor.newInstance();
        FileInputStream fis = new FileInputStream("singleton.obj");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Hungry s1 = (Hungry) ois.readObject();

        ois.close();
        System.out.println(s + "\n" + s1);
        System.out.println("序列化前后两个是否同一个：" + (s == s1));
    }

}
